
$(function () {
   
    initData();
});

function initData(){
    //初始化首页数据
    url = $.util.getRequestJson('/json/dataQuery.storeDataInfo.json');
    $.util.get(url, function (data) {
        $('#gatherScore').html(data.data.gatherScore + '<span>分</span>');
        $("#noFinishMoney").html(data.data.noFinishMoney + '<span>分</span>');
        $('#exchangeNum').html(data.data.exchangeNum + '<span>笔</span>');
        $("#historySumMoney").html(data.data.historySumMoney + '<span>分</span>');
        $("#hisroryExchangeNum").html(data.data.historyExchangeNum + '<span>笔</span>');
    });
    $.util.get($.util.getRootPath() + "/consumeManage/unchangeOrderNum", function (data) {
        $("#unchangeNum").html('('+data.data.TOTAL+')' );

    });
    //初始化未完成账单表格列表
    initDatagrid();
}

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/consumeManage.orderList.json');
    url = url + "?orderStatus=0&rows=10";
    $('#listTable').datagrid({
            url: url,
            idField: 'id',
            fit: true,
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            border: true,
            toolbar: '#toolbarButton',
            fitColumns: true,
            columns: [
                [
                    {
                        field: 'orderNo',
                        title: '订单编号',
                        align: 'center'
                    },
                    {
                        field: 'phone',
                        title: '客户手机号',
                        align: 'center',
                        width: 13,
                        formatter:function (value, row, index) {
                            return value.toString().substr(0,3)+"****"+value.toString().substr(7);
                        }
                    },
                    {
                        field: 'name',
                        title: '客户姓名',
                        align: 'center',
                        width: 13
                    },
                    {
                        field: 'expendScore',
                        title: '消费积分',
                        align: 'center',
                        width: 8
                    },
                    {
                        field: 'totalNum',
                        title: '消费数量',
                        align: 'center',
                        width: 8
                    },
                    {
                        field: 'expendTime',
                        title: '消费时间',
                        align: 'center',
                        width: 13
                    },
                    {
                        field: 'status',
                        title: '结算状态',
                        align: 'center',
                        width: 13,
                        formatter: function (value, row) {
                            if (row.status == 0) {
                                aEl = ' <span style="color:orange ">未兑换</span>';
                            } else if (row.status == 1) {
                                aEl = ' <span style="color: limegreen">已兑换</span>';
                            }
                            return $.string.format(aEl);
                        }
                    }, {
                    field: 'id',
                    title: '操作',
                    align: 'center',
                    width: 13,
                    formatter: function (value, row) {
                        var aEl = '<a href=javascript:exchangeProduct("{1}"); class="easyui-tooltip " title="兑换">兑换</a>';
                        aEl += ' | <a href=javascript:orderDetail("{0}"); class="easyui-tooltip " title="详情">详情</a>';
                        //格式化操作按钮
                        return $.string.format(aEl, row.id, row.orderNo);
                    }
                }
                ]
            ]
        }
    )
    ;
}

// 查看订单详情   订单id
function orderDetail(orderId) {

    var title = "详情";
    var url = $.util.getRootPath() + '/page/shopManager/order/orderDetail.html';
    url = orderId ? url + '?id=' + orderId : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 650,
        height: 400,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton: false,
        closeButtonText: '关闭'
    });
}

// 兑换商品  参数为订单id
function exchangeProduct(orderNo) {
    $.messager.confirm('确认信息', '确定要兑换吗？', function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/storeHandle.gatherByOrder.json');
            $.util.post(url, {payCode: orderNo}, function (json) {
                $.easyui.messager.show(json.message);
                if (json.success) {
                    initData();
                }
            });
        }
    });
}

//点击查看更多
function query() {
    var title = '订单处理';
    var url = $.util.getRootPath() + '/page/shopManager/order/orderList.html';
    //  url = url + '?type=0',
    if (parent.$.mainpage) {
        parent.$.mainpage.mainTabs.addModule(title, url);
    } else {
        window.open(url, "_blank");
    }
}